import { Component, OnInit } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd';
import { SysParamService } from '@core/routes-services/sys-param.service';


@Component({
  selector: 'app-sys-param-list',
  templateUrl: './sys-param-list.component.html',
})
export class SysParamListComponent implements OnInit {

  isLoading: boolean = false;
  keyword: any;
  page: any = { number: 1, size: 100, totalElements: 0, content: [] };

  constructor(private message: NzMessageService,
              private sysParamService: SysParamService) {
  }

  ngOnInit() {
    this.initPage();
    /*{name:"json配置",value:'{}',type:'json'},
      {name:"array数组",value:"100,200,300,400,500,600,700,800,900,1000",type:'array'},
      {name:"string字符串",value:null,type:'string'},
      {name:"数字",value:null,type:'number'},
      {name:"字符串3",value:"2sfaklsd",type:'string'}*/
  }

  initPage() {
    this.isLoading = true;
    this.sysParamService.pagination(this.page, this.keyword).then((res) => {
      // console.log(res);
      if (res.status.toString().startsWith('20')) {
        this.page = res.data;
        ++this.page.number;
      } else {
        this.message.warning(res.message);
      }
      this.isLoading = false;
    }, error => {
      this.message.error(error.message ? error.message : error.error);
      this.isLoading = false;
    });
  }

  /**
   * 搜索重置分页数据
   */
  search() {
    this.page = { number: 0, size: 100, totalElements: 0, content: [] };
    this.initPage();
  }

  changePageNo(pageNo) {
    this.page = { number: pageNo, size: 100, totalElements: 0, content: [] };
    this.initPage();
  }

  entityChanges(e) {
    this.sysParamService.saveOrUpdateEntity(e).then((res) => {
      if (res.status.toString().startsWith('20')) {
        this.message.success('保存成功');
      } else {
        this.message.warning(res.message);
      }
    }, (error) => {
      console.error('保存出错：', error);
      this.message.error(error.message ? error.message : error.error);
    });
  }
}
